<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_Model extends CI_Model {
 public function __construct()
 {
  parent::__construct();
 }
 function login($email,$password)
 {
  $this->db->where("email",$email);
  $this->db->where("password",$password);

  $query=$this->db->get("user");
  if($query->num_rows()>0)
  {
   foreach($query->result() as $rows)
   {
    //add all data to session
    $newdata = array(
      'user_id'  => $rows->id,
      'user_name'  => $rows->username,
      'user_email'    => $rows->email,
      'logged_in'  => TRUE,
    );
   }
   $this->session->set_userdata($newdata);
   return true;
  }
  return false;
 }
 public function add_user()
 {
  $data=array(
    'username'=>$this->input->post('user_name'),
    'email'=>$this->input->post('email_address'),
    'password'=>md5($this->input->post('password'))
  );
  $this->db->insert('user',$data);
 }
 
 function getDiagramData()
 {
  $result = $this->session->all_userdata();

   $this->db->select('diagramid, diagramname,diagramlink');
  // $this->db->from('mytable');
  $this->db->where('id', $this->session->userdata('user_id'));
  $this->db->where('userid', $this->session->userdata('user_id'));
  $query = $this->db->get('user, diagram');
  
  $stack = array();
  foreach($query->result() as $rows)
   {
    //add all data to session
    $newdata = array(
      'diagram_id'    => $rows->diagramid,
      'diagram_name'  => $rows->diagramname,
      'diagram_link'    => $rows->diagramlink,
	  //'user_id'  => $rows->userid,
    );
	array_push($stack, $newdata);
	//echo $rows->diagramname;
   }
  
  //echo $result['user_email'];
  //echo $result['user_name'];
  //echo $newdata['diagram_name'];
  //echo count($stack);
  return $stack;
 }
}
?>